<template>
  <component :is="linkType" v-bind="linkProps(to)">
    <slot></slot>
  </component>
</template>

<script setup lang="ts">
import { isExternal } from '@/utils/utils'
const props = defineProps({
  to: {
    type: Object,
    required: true
  }
})

const isExternalLink = computed(() => {
  return isExternal(props.to.path || '')
})
const linkType = computed(() => (isExternalLink.value ? 'a' : 'router-link'))
const linkProps = (to: string) => {
  if (isExternalLink.value) {
    return {
      href: to.path,
      target: '_blank',
      rel: 'noopener noreferrer'
    }
  }
  return {
    to: to
  }
}
</script>

<style lang="scss" scoped></style>
